package com.ir;

import com.ast.LValNode;
import com.ast.NumberNode;
import com.sysdep.CodeGenerator;

public class UnaryExp extends Expr {
    String op;
    Expr exp;
    NumberNode node;
    LValNode lVal;

    public UnaryExp(String op, Expr exp) {
        this.op = op;
        this.exp = exp;
    }

    public UnaryExp(NumberNode node) {
        this.node = node;
    }

    public UnaryExp(LValNode lVal) {
        this.lVal = lVal;
    }

    public String getOp() {
        return op;
    }

    public Expr getExp() {
        return exp;
    }

    public String toString() {
        if (lVal == null)
            return "Unary(" + op + " " + exp + ")";
        else
            return "Unary(" + lVal + ")";
    }

    @Override
    public boolean accept(CodeGenerator cg) {
        return cg.visit(this);
    }

    @Override
    public int getIntValue() {
        switch (op) {
            case "+":
                return exp.getIntValue();
            case "-":
                return -exp.getIntValue();
            default:
                throw new Error("Unknown operator " + op);
        }
    }
}
